package cn.stylefeng.guns.sys.modular.dbServicelist.timer;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.sys.modular.dbServicelist.entity.DbServicelist;
import cn.stylefeng.guns.sys.modular.dbServicelist.service.DbServicelistService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-08-25 16:59
 */
//@Configuration      //1.主要用于标记配置类，兼备Component的效果。
//@EnableScheduling   // 2.开启定时任务
public class dbListTimer {

    @Autowired
    private DbServicelistService dbServicelistService;

    @Scheduled(fixedRate=10000)
    private void configureTasks() throws ParseException {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
        List<DbServicelist> list = dbServicelistService.list(new QueryWrapper<DbServicelist>().ne("state","2"));
        List<DbServicelist> dbServicelistList = new ArrayList<>();
        for (DbServicelist dbServicelist : list) {
            Date parse = simpleDateFormat.parse(dbServicelist.getEffectiveTime());
            if ( parse.before(new Date())){
                dbServicelistList.add(dbServicelist);
            }
        }
        if (dbServicelistList !=null && dbServicelistList.size() !=0){
            for (DbServicelist dbServicelist : dbServicelistList) {
                dbServicelist.setState("2");
                dbServicelist.setUpdatedBy("admin");
                dbServicelist.setUpdatedTime(new Date());
                dbServicelistService.updateById(dbServicelist);
            }
        }

        System.out.println("执行静态定时任务时间: " + LocalDateTime.now()+"已完成对数据库的定时任务，将过期数据更改为已过期状态");
    }
}
